package cn.fl.repay.service.impl;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.repay.constant.RepayConstant;
import cn.fl.repay.service.IInsOfflineRepayDtlService;
import cn.fl.repay.service.OfflineRepayStrategy;
import cn.fl.repay.vo.InsOfflineRepayDtlVO;
import cn.fl.repay.vo.InsOfflineRepayLogVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 蓝海银行线下还款可能存在贴息
 */
@Service
public class LanHaiBankOfflineRepayStrategy implements OfflineRepayStrategy {

    @Autowired
    private IInsOfflineRepayDtlService insOfflineRepayDtlService;

    @Override
    public boolean support(String repayType) {
        return true;
    }

    @Override
    public List<InsOfflineRepayDtlVO> getRepayDtl(String crCntNo, String repayType, String actPayDt) {
        BaseLogUtils.newLogger("蓝海银行线下还款试算Request Params: crCntNo={}, repayType={}, actPayDt={}"
                , crCntNo, repayType, actPayDt).info();
        List<InsOfflineRepayDtlVO> repayDtlVOList = null;
        if (repayType.equals(RepayConstant.REPAY_TYP_CD_SETTLE)) {
            repayDtlVOList = insOfflineRepayDtlService.getLaHaiBankSettleRepayDtl(crCntNo, actPayDt);
        } else {
            repayDtlVOList = insOfflineRepayDtlService.getLaHaiBankStageRepayDtl(crCntNo, actPayDt);
        }
        return repayDtlVOList;
    }

    @Override
    public void checkRepayData(InsOfflineRepayLogVO repayLogVO, List<InsOfflineRepayDtlVO> repayDtlList) {
        BaseLogUtils.newLogger("蓝海线下还款校验还款数据, RepayLogVO: {}, RepayDtlVOList: {}"
                , JSON.toJSONString(repayLogVO), JSON.toJSONString(repayDtlList)).debug();
        // 分期要求单笔全额支付
        if(RepayConstant.REPAY_TYP_CD_PD.equals(repayLogVO.getRepayTypCd())){
            String actPayDt = DateUtils.formatDateToString(repayLogVO.getActPayDt(), DateUtils.YYYY_MM_DD);
            List<InsOfflineRepayDtlVO> needRepayDtlVOList =
                    insOfflineRepayDtlService.getLaHaiBankStageRepayDtl(repayLogVO.getCrCntNo(), actPayDt);
            if(CollectionUtils.isEmpty(needRepayDtlVOList)) {
                throw new BaseException(BaseExceptionConstant.COMMON_0001_NAME, "获取账单异常，请稍后再试！");
            }
            if (needRepayDtlVOList.size() > repayDtlList.size()) {
                throw new BaseException(BaseExceptionConstant.COMMON_0001_NAME, "请勾选所有已到还款日的账单明细");
            }
        }
        // 分期和结清都不能存在逾期保费为负数的情况
        for (InsOfflineRepayDtlVO insOfflineRepayDtlVO : repayDtlList) {
            Long ovdGrtAmt = insOfflineRepayDtlVO.getOvdGrtAmt();
            if (ovdGrtAmt != null && ovdGrtAmt < 0) {
                throw new BaseException(BaseExceptionConstant.COMMON_0001_NAME, "实还逾期担保费小于0，无法核销，请走线下调账流程");
            }
            Long grtAmt = insOfflineRepayDtlVO.getGrtAmt();
            if (grtAmt != null && grtAmt < 0) {
                throw new BaseException(BaseExceptionConstant.COMMON_0001_NAME, "实还担保费小于0，无法核销，请走线下调账流程");
            }
        }
    }
}
